package com.mohe.zhihui.utils;

import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;

/**
 * 通过JSON插件往页面回写数据
 * 
 * @author zlc
 * 
 */
public class JsonUtil {
	public static void writeJson(Object object) {
		String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
		json = json.replace("\t", "\\t"); // 字符串含有制表符但是没有进行转义，导致前台ajax执行error函数
//		json = "{\"list\":[],\"map\":[{\"content\":\"活动6\",\"date\":\"2016-01-27 14:57:26\",\"index\":\"9\",\"timeKey\":\"6\",\"title\":\"活动6\"},{\"content\":\"活动7\",\"date\":\"2016-01-27 14:56:33\",\"index\":\"10\",\"timeKey\":\"7\",\"title\":\"活动7\"},{\"content\":\"活动8\",\"date\":\"2016-01-27 14:55:54\",\"index\":\"11\",\"timeKey\":\"9\",\"title\":\"活动8\"},{\"content\":\"活动10<p style=text-align:center;>s</p>\",\"date\":\"2016-01-27 14:55:15\",\"index\":\"12\",\"timeKey\":\"10\",\"title\":\"活动10\"},{\"content\":\"活动11\",\"date\":\"2016-01-27 14:54:28\",\"index\":\"13\",\"timeKey\":\"12\",\"title\":\"活动11\"},{\"content\":\"活动12\",\"date\":\"2016-01-27 14:53:37\",\"index\":\"14\",\"timeKey\":\"13\",\"title\":\"活动12\"},{\"content\":\"活动18\",\"date\":\"2016-01-27 14:52:27\",\"index\":\"15\",\"timeKey\":\"18\",\"title\":\"活动18\"},{\"content\":\"活动17\",\"date\":\"2016-01-27 14:51:21\",\"index\":\"16\",\"timeKey\":\"17\",\"title\":\"活动17\"}],\"object\":{\"count\":20,\"currPage\":2,\"currentCount\":4,\"pageCount\":3,\"pageSize\":8},\"success\":true,\"type\":0}";
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
}
